package thread;

public class Printabcd {
	public static void main(String[] args) {
		Thread th1 = new mythread(1);
		th1.start();
		Thread th2 = new mythread(2);
		th2.start();
		Thread th3 = new mythread(3);
		th3.start();
	}
}

class mythread extends Thread {
	public static String str = "C";
	private static int i = 0;
	private int id = 0;

	public mythread(int id) {
		this.id = id;
	}

	public void run() {
		try {
			// 三个线程要使用同一个共享对象来进行加锁和解锁.
			// 方法前面使用synchronized使用的是当前对象来加锁, 所以三个不同的线程他们的对象锁是不一样的.
			synchronized (this.getClass()) {
				System.out.println(this.getClass());
				while (i < 10) {
					//加与不加this.getClass().notifyAll();this.getClass().wait();
					this.getClass().notifyAll();
					if (str.endsWith("C")) {
						System.out
								.println("ID:" + id + ", Times:" + i + " : A");
						str = str + "A";
						this.getClass().wait();
					}

					if (str.endsWith("A")) {
						System.out
								.println("ID:" + id + ", Times:" + i + " : B");
						str = str + "B";
						this.getClass().wait();
					}

					if (str.endsWith("B")) {
						System.out
								.println("ID:" + id + ", Times:" + i + " : C");
						str = str + "C";
						i++;
						this.getClass().wait();
					}

				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}


